﻿using System.Net;
using System.Net.Sockets;
using System.Text;


class UdpClientExample
{
    private UdpClient sender;
    private IPEndPoint endPoint;
    private Thread sendThread;
    private bool isSending = false;

    public UdpClientExample(string ipAddress, int port)
    {
        sender = new UdpClient();
        IPAddress ip = IPAddress.Parse(ipAddress);
        endPoint = new IPEndPoint(ip, port);
    }

    public void StartSending()
    {
        isSending = true;
        sendThread = new Thread(new ThreadStart(SendData));
        sendThread.Start();
    }

    public void StopSending()
    {
        isSending = false;
        sendThread.Join(); // 等待线程结束
        sender.Close(); // 关闭UdpClient
    }

    private void SendData()
    {
        Random rand = new Random();
        int index = 0;
        DateTime dt = DateTime.Now;
        byte[] spectrumData = CreateSepctrumData(0, DateTime.Now);
        int channelNo = 0;
        

        while (isSending)
        {
            byte[] byteArray = null;
            int offset = 0;
            byte[] sendBytes = new byte[2 + 256 + 8];
            sendBytes[0] = 0x08;
            sendBytes[1] = 0x20;
            offset += 2;

            for(int i = 0; i < 256/2; i++)
            {
                ushort value = (ushort)rand.Next(5000, 45000);
                byteArray = BitConverter.GetBytes(value);
                Array.Copy(byteArray,0,sendBytes,offset,2);
                offset += 2;
            }
            
            if(index < 375)
            {
                Array.Copy(spectrumData, index * 6, sendBytes, offset, 6);
                offset += 6;
                short s = (short)(channelNo << 12 | index);
                byteArray = BitConverter.GetBytes(s);
                //Array.Copy(byteArray, 0, sendBytes, offset, 2);
                sendBytes[offset] = byteArray[1];
                offset += 1;
                sendBytes[offset] = byteArray[0];
                offset += 1;
            }
            else if(index > 375 + 75)
            {
                index = -1;
                spectrumData = CreateSepctrumData(channelNo,dt);
                channelNo++;
                if (channelNo >= 16)
                {
                    channelNo = 0;
                }
            }
            index++;

            sender.Send(sendBytes, sendBytes.Length, endPoint);

            Thread.Sleep(10); // 每秒发送一次，可以根据需要调整
        }
    }

    static void Main()
    {
        UdpClientExample udpSender = new UdpClientExample("192.168.1.118", 4010);
        udpSender.StartSending();

        Console.WriteLine("输入回车停止发送...");
        Console.ReadLine();

        udpSender.StopSending();
    }

    private static byte[] CreateSepctrumData(int channelNo,DateTime dt)
    {
        Random random = new Random();

        byte[] resBytes = new byte[2250];

        int offset = 0;
        // 序列号
        byte[] byteArray = Encoding.ASCII.GetBytes("OSI172");
        Array.Copy(byteArray, 0, resBytes, offset, byteArray.Length);
        offset += 16;

        // 软硬件版本
        byteArray = Encoding.ASCII.GetBytes("V152461232");
        Array.Copy(byteArray, 0, resBytes, offset, byteArray.Length);
        offset += 16;

        // 通道数
        byteArray = BitConverter.GetBytes((short)16);
        Array.Copy(byteArray, 0, resBytes, offset, 2);
        offset += 2;

        // 转发器版本
        byteArray = BitConverter.GetBytes((short)1);
        Array.Copy(byteArray, 0, resBytes, offset, 2);
        offset += 2;

        // 当前温度
        short tmp = (short)random.Next(2000, 3000);
        byteArray = BitConverter.GetBytes(tmp);
        Array.Copy(byteArray, 0, resBytes, offset, 2);
        offset += 2;

        // 运行分钟
        TimeSpan ts = DateTime.Now - dt;
        byteArray = BitConverter.GetBytes((short)ts.TotalMinutes);
        Array.Copy(byteArray, 0, resBytes, offset, 2);
        offset += 2;

        // 预留
        offset += 20;

        // 通道号
        resBytes[offset] = (byte)channelNo;
        offset += 1;

        // 光栅数
        resBytes[offset] = (byte)8;
        offset += 1;

        for(int i = 0; i < 8; i++)
        {
            int value = random.Next(1525000, 1565000);
            byteArray = BitConverter.GetBytes(value);
            Array.Copy(byteArray, 0, resBytes, offset, 4);
            offset += 4;
        }

        offset += 88;

        for (int i = 0; i < 8; i++)
        {
            short value = (short)((random.NextDouble() -50 + channelNo)*10);
            byteArray = BitConverter.GetBytes(value);
            Array.Copy(byteArray, 0, resBytes, offset, 2);
            offset += 2;
        }

        offset += 44;

        offset += 3;
        offset += 1;

        for(int i = 0; i < 1001; i++)
        {
            ushort value = (ushort)((random.NextDouble() - 50 + channelNo) * 10);
            byteArray = BitConverter.GetBytes(value);
            //Array.Copy(byteArray, 0, resBytes, offset, 2);
            //offset += 2;
            resBytes[offset] = byteArray[0]; offset += 1;
            resBytes[offset] = byteArray[1]; offset += 1;
        }

        resBytes[offset] = (byte)channelNo;
        offset += 1;

        resBytes[offset] = 0xfa;
        offset += 1;

        return resBytes;
    }
}
